package com.blog.nav.controller;

import com.blog.common.entity.Result;
import com.blog.nav.entry.NavigationDTO;
import com.blog.nav.entry.NavigationPO;
import com.blog.nav.service.Impl.NavigationServiceImpl;
import com.blog.nav.service.NavigationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/nav")
public class NavigationController {

    @Autowired
    private NavigationService navigationService;

    @GetMapping("/getNav/{status}")
    public Result<List<NavigationDTO>> getNav(@PathVariable(name = "status")int status){
        List<NavigationPO> navByStatus = navigationService.findNavByStatus(status);
        List<NavigationDTO> dots = new ArrayList<>();
        navByStatus.forEach((nav)->{
            if (nav.getHasChild()){
                List<NavigationDTO> child = new ArrayList<>();
                navByStatus.forEach((cnav)->{
                    if (cnav.getFatherId()!=null&&cnav.getFatherId().equals(nav.getId())){
                        child.add(new NavigationDTO(cnav.getId(),cnav.getName(),false,null,cnav.getStatus()));
                    }
                });
                dots.add(new NavigationDTO(nav.getId(),nav.getName(),true,child,nav.getStatus()));
            }
        });
        return new Result(dots);
    }

    @DeleteMapping("/deleteNav/{id}")
    public Result deleteNav(@PathVariable(name = "id")Long id){
        navigationService.deleteById(id);
        return new Result();
    }

    @PostMapping("addNav")
    public Result addNav(@RequestBody NavigationPO navigationPO){
        navigationService.add(navigationPO);
        return new Result();
    }

    @PutMapping("updateNav")
    public Result updateNav(@RequestBody NavigationPO navigationPO){
        navigationService.update(navigationPO);
        return new Result();
    }



}
